package com.example.truyencotich.support;

import java.io.IOException;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;

/*
 * class quản lý âm thanh
 * */

public class SoundManager {
	MediaPlayer mp;
	Context mContext;

	public SoundManager(Context c) {
		this.mContext = c;
		mp = new MediaPlayer();
	}

	public void playSound(String fileName) {
		if (!Global.isMute) {
			if (mp != null) {
				if (mp.isPlaying()) {
					mp.stop();
					mp.release();
				}
				mp = new MediaPlayer();
			} else
				mp = new MediaPlayer();
			try {
				AssetFileDescriptor descriptor = mContext.getAssets().openFd(
						fileName);
				mp.setDataSource(descriptor.getFileDescriptor(),
						descriptor.getStartOffset(), descriptor.getLength());
				mp.prepare();
			} catch (IOException e) {
				e.printStackTrace();
			}
			mp.start();
		}
	}

	public void stopSound() {
		if (mp != null) {
			if (mp.isPlaying())
				mp.stop();
			mp.release();
			mp = null;
		}
	}

	public void pauseSound() {
		if (mp != null) {
			if (mp.isPlaying())
				mp.pause();
		}
	}

	public void resumeSound() {
		if (mp != null) {
			if (!mp.isPlaying())
				mp.start();
		}
	}
}
